/**
 * glue.cpp
 *
 * @auther Miles Dung
 * @date May 26 2011
 *
 * This file is part of CuteTransfer project, which
 * released under the GNU GPL v2 or v3 (whichever you like)
 * This project is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY, without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU General Public License for more details.
 */
#include "core.h"
#include "glue.h"
#include "Client.h"

//map filepath with row
static std::map<std::string, int> map_file;
static std::map<std::string, int>::iterator it;
static int row = 0;

int recv_message(SOCKADDR_IN& sa_in, const char* mes)
{
    /*send local varible addr t may cause error
        signals and slots are not exec instantly
    YOU MUSTN'T passing arguments as ptr or reference!*/

    unsigned short nport = sa_in.sin_port;
    unsigned short hport = ntohs(nport);
    Core::getCore()->emitSignalAppendMessage(sa_in.sin_addr.S_un.S_addr, hport, mes);
    return 0;
}

int send_message(const char* ip, unsigned short port, const std::string& mes)
{
    //    in_addr sip;
    //    sip.S_un.S_addr = ip;
    Client::getSingleClient()->startCon(port, ip, 0, mes);
    return 0;
}


int recv_file_request()
{
    return 0;
}

int recv_file_init(SOCKADDR_IN& sa_in, FileInfo& finfo)
{
    std::string filepath;
    filepath.append(getenv("HOMEDRIVE")); //nt6 homepath C:
    filepath.append(getenv("HOMEPATH")); // \Users\Administrator
    if (filepath[filepath.length()-1]!='\\')
    {
        filepath.append("\\");
    }
    filepath.append(finfo.filename);

    char buf[4];

    map_file.insert(std::make_pair(filepath, row));

    Core::getCore()->emitSignalTableChange(row, 0, "R");
    Core::getCore()->emitSignalTableChange(row, 1, finfo.filename);
    Core::getCore()->emitSignalTableChange(row, 2, "starting...");
    Core::getCore()->emitSignalTableChange(row, 3, inet_ntoa(sa_in.sin_addr));
    Core::getCore()->emitSignalTableChange(row, 4, finfo.filesize);

    row++;
    return 0;
}

int recv_file(const std::string& filepath, int progress)
{
    std::map<std::string, int>::iterator it;
    it = map_file.find(filepath);
    if (it == map_file.end())
    {
        std::cerr << "find FILEPATH:" << filepath
                  << "\not in map list" << std::endl;
        return -1;
    }
    int row = it->second;
    char buf[4]; // here passing buf may cause error!
    Core::getCore()->emitSignalTableChange(row, 2, progress);
    return 0;
}

int send_file(const std::string& filepath, int progress)
{
    std::map<std::string, int>::iterator it;
    it = map_file.find(filepath);
    if (it == map_file.end())
    {
//        QMessageBox::warning(0, QString::fromAscii("Send File Not Found"),
//                             QString::fromStdString(filepath));
        std::cerr << "find FILEPATH:" << filepath
                  << "\not in map list" << std::endl;
        return -1;
    }
    int row = it->second;
    char buf[4];
    Core::getCore()->getMainWindow()->tableChange(row, 2, itoa(progress, buf, 10));
    return 0;
}
int send_file_request(const char* ip, unsigned short port, const std::string& filepath)
{
    Client::getSingleClient()->startCon(port, ip, 1, filepath);

    return 0;
}

int send_file_init(SOCKADDR_IN &sa_in, const std::string &filepath, FileInfo& finfo)
{
    map_file.insert(std::make_pair(filepath, row));

    char buf[4];

    Core::getCore()->getMainWindow()->tableChange(row, 0, "S");
    Core::getCore()->getMainWindow()->tableChange(row, 1, finfo.filename);
    Core::getCore()->getMainWindow()->tableChange(row, 2, "starting...");
    Core::getCore()->getMainWindow()->tableChange(row, 3, inet_ntoa(sa_in.sin_addr));
    Core::getCore()->getMainWindow()->tableChange(row, 4, itoa(finfo.filesize, buf, 10));

    row++;
    return 0;
}

